package com.parking.common.core.paracheck.checker;

import com.parking.common.core.paracheck.JustTwoDecimal;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;
import java.util.regex.Pattern;

/**
 * 自定义注释 BigDecimal至多两位小数检测器
 * */
public class JustTwoDecimalChecker implements ConstraintValidator<JustTwoDecimal, BigDecimal> {
    /**
     * BigDecimal两位小数校验方法
     * */
    @Override
    public boolean isValid(BigDecimal price, ConstraintValidatorContext constraintValidatorContext) {
        if(price==null) return true;

        String priceStr = price.toString();
        Pattern pattern = Pattern.compile("^[+]?([0-9]+(.[0-9]{1,2})?)$");
        return pattern.matcher(priceStr).matches();
    }
}
